#coding:utf-8

# 单线程执行任务队列

import time
#兼容python2与python3
try:
    from Queue import Queue
except:
    from queue import Queue

import requests
from lxml import etree


class DoubanSpider(object):
    def __init__(self):
        self.url_list = ["https://movie.douban.com/top250?start=" + str(page) for page in range(0, 226, 25)]
        self.headers = {"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"}
        self.queue = Queue()

    def send_request(self, url):
        print("[Info]:send request...{}".format(url))
        response = requests.get(url, headers=self.headers)
        #time.sleep(1)  #测试休眠用
        return response

    def parse_response(self, response):
        html = response.content
        html_obj = etree.HTML(html)
        node_list = html_obj.xpath("//div[@class='info']")

        for node in node_list:
            title = node.xpath("./div[1]/a/span[1]/text()")[0]
            score = node.xpath(".//span[@class='rating_num']/text()")[0]
            self.queue.put(title + "\t" + score)

    def main(self):
        for url in self.url_list:
            response = self.send_request(url)
            self.parse_response(response)


if __name__ == "__main__":
    spider = DoubanSpider()
    start = time.time()
    spider.main()
    end = time.time()
    print("[Info]: using time {}".format(end-start))


